package com.itgone.model.impl;

import com.github.lazylibrary.util.StringUtils;
import com.itgone.common.MallAPI;
import com.itgone.model.IProductModel;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.builder.PostFormBuilder;
import com.zhy.http.okhttp.callback.Callback;

import static com.itgone.common.Const.KEY_CATEGORY_ID;
import static com.itgone.common.Const.KEY_KEYWORD;
import static com.itgone.common.Const.KEY_PAGE_NUM;
import static com.itgone.common.Const.KEY_PAGE_SIZE;
import static com.itgone.common.Const.KEY_PRODUCT_ID;

public class ProductModelImpl implements IProductModel {

    @Override
    public void list(String categoryId, String keyword, String pageNum, String pageSize, Callback callback) {
        PostFormBuilder postFormBuilder = OkHttpUtils.post().url(MallAPI.ProductAPI.LIST);

        if (!StringUtils.isBlank(categoryId)) {
            postFormBuilder.addParams(KEY_CATEGORY_ID, categoryId);
        }
        if (!StringUtils.isBlank(keyword)) {
            postFormBuilder.addParams(KEY_KEYWORD, keyword);
        }
        if (!StringUtils.isBlank(pageNum)) {
            postFormBuilder.addParams(KEY_PAGE_NUM, pageNum);
        }
        if (!StringUtils.isBlank(pageSize)) {
            postFormBuilder.addParams(KEY_PAGE_SIZE, pageSize);
        }

        postFormBuilder.build().execute(callback);
    }

    @Override
    public void getProduct(String productId, Callback callback) {
        OkHttpUtils
                .post()
                .url(MallAPI.ProductAPI.DETAIL)
                .addParams(KEY_PRODUCT_ID, productId)
                .build()
                .execute(callback);
    }
}
